//
//  WebViewController.m
//  helloword
//
//  Created by chen on 17/2/23.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "WebViewController.h"
#import "WebKit/WebKit.h"

@interface WebViewController ()<WKNavigationDelegate>

@property(nonatomic,strong) WKWebView *webView;

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithFrame];
    // Do any additional setup after loading the view.
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma marik -- initWithFrame初始化视图
- (void)initWithFrame{
    //按钮栏
    //按钮宽度
    
    CGRect screen = [[UIScreen mainScreen] bounds];
    CGFloat buttonVarWidth = 316;
    UIView *buttonBar = [[UIView alloc] initWithFrame:CGRectMake((screen.size.width - buttonVarWidth)/2, 80, buttonVarWidth, 30)];
    [self.view addSubview:buttonBar];
    
    //1.添加LoadHTMLString按钮
    UIButton *buttonLoadHTMLString = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadHTMLString setTitle:@"LoadHTMLString" forState:UIControlStateNormal];
    buttonLoadHTMLString.frame = CGRectMake(0, 0, 117,30);
    [buttonLoadHTMLString addTarget:self action:@selector(testLoadHTML:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadHTMLString];
    //2.添加LoadData按钮
    UIButton *buttonLoadData = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadData setTitle:@"LoadData" forState:UIControlStateNormal];
    buttonLoadData.frame = CGRectMake(137, 0, 67, 30);
    [buttonLoadData addTarget:self action:@selector(testLoadData:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadData];
    //3.添加LoadRequest按钮
    UIButton *buttonLoadRequest = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonLoadRequest setTitle:@"LoadRequest" forState:UIControlStateNormal];
    buttonLoadRequest.frame = CGRectMake(224, 0, 92, 30);
    [buttonLoadRequest addTarget:self action:@selector(testLoadRequest:) forControlEvents:UIControlEventTouchUpInside];
    [buttonBar addSubview:buttonLoadRequest];

    
    //4.添加WKWebView视图
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 110, screen.size.width, screen.size.height-80)];
    [self.view addSubview:_webView];
}

- (void)testLoadHTML:(id)sender{
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSLog(@"-----%@",htmlPath);
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSError *error = nil;
    NSString *html = [[NSString alloc] initWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:&error];
    if (error == nil) {
        [self.webView loadHTMLString:html baseURL:bundleUrl];
    }
}
- (void)testLoadData:(id)sender{
    NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
    NSData *htmlData = [[NSData alloc] initWithContentsOfFile:htmlPath];
    [self.webView loadData:htmlData MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:bundleUrl];
}
- (void)testLoadRequest:(id)sender{
    NSURL *url = [NSURL URLWithString:@"http://www.51work6.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    self.webView.navigationDelegate = self;
}

#pragma mark -- 实现WKNavigationDelegate委托协议
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"开始加载");
}
-(void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    NSLog(@"内容开始返回");
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"加载完成");
}
-(void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"加载失败error:%@",error);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
